<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>

<body>
    <style type="text/css">
        .layui-table-cell {
            height: auto;
            line-height: 70px;
        }
    </style>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn data-add-btn" lay-event="add"> 添加记录 </button>
                    <button class="layui-btn layui-btn-danger data-delete-btn" lay-event="delete" id="delete"> 批量删除
                    </button>
                </div>
            </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
                <form class="layui-form">
                    <!-- <div class="layui-form-item"> --><a
                        class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
                    <!-- </div> -->
                </form>
            </script>

        </div>
    </div>
    <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="../js/lay-config.js?v=2.0.0" charset="utf-8"></script>
    <script>
        layui.use(['form', 'table', 'cookie'], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer,
                cookie = layui.cookie,
                table = layui.table;
            //超时跳转
            $.ajaxSetup({
                complete: function (XMLHttpRequest, textStatus) {
                    if (textStatus == "parsererror") {
                        //这里不能用window.location,href，因为用了之后你会发现他是在子窗口跳转到登录页，而不是整个系统跳转
                        top.location.href = '../../views/login.html';
                    }
                }
            })
            // 放大图片
            var tableIns = table.render({
                elem: '#currentTableId',
                url: '/system',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [
                    [{
                            type: "checkbox",
                            width: 50
                        },
                        {
                            field: 'charge_unit',
                            width: 200,
                            title: '收款方'
                        },
                        {
                            field: 'card_id',
                            width: 200,
                            title: '卡号',
                        },
                        {
                            field: 'bankname',
                            width: 200,
                            title: '开户银行',
                        },
                        {
                            title: '收款码',
                            minWidth: 150,
                            align: 'center',
                            templet: function (d) {
                                var url = d.qr_url;
                                return '<img src="' + url +
                                    '" style="height:70px" title="点击查看" onclick="showBigImage(this)"/>';
                            }
                        },
                        {
                            width: 120,
                            title: '启/停用',
                            align: "center",
                            // 加载开关状态
                            templet: function (d) {
                                if (d.is_del === 0) {
                                    return '<p  newid="' + d.id +
                                        '" onclick="switchChange(this)"><input type="checkbox" id="switch" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="已启用|未启用"></p>';
                                } else {
                                    return '<p  newid="' + d.id +
                                        '" onclick="switchChange(this)"><input type="checkbox" id="switch" name="open" lay-skin="switch" checked lay-filter="switchTest" lay-text="已启用|未启用"></p>';
                                }

                            }
                        },
                        {
                            title: '操作',
                            minWidth: 150,
                            toolbar: '#currentTableBar',
                            align: "center",
                            fixed: "right"
                        },
                    ]
                ],
                // skin: 'line'
            });
            showBigImage = function (e) {
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true, //点击阴影关闭
                    area: ['350px'], //宽高
                    content: "<img src=" + $(e).attr('src') + " width=100% height=100%/>"
                });
            }
            // 切换开关
            switchChange = function (e) {
                var id = $(e).attr('newid');
                var that = $(e).children()[0];
                var is_del = (that.checked) ? is_del = 1 : is_del = 0;
                var str = {
                    "id": id,
                    "is_del": is_del
                };
                str = JSON.stringify(str);
                $.ajax({
                    url: '/system/switch',
                    method: "PUT",
                    data: str,
                    contentType: "application/json",
                    success: function (data) {
                        layer.msg("修改成功！", {
                            icon: 1
                        });
                        tableIns.reload();
                    },
                    error: function (err) {
                        layer.msg("服务器内部错误", {
                            icon: 2
                        });
                    }
                });


            }
            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var result = JSON.stringify(data.field);
                layer.alert(result, {
                    title: '最终的搜索信息'
                });

                //执行搜索重载
                table.reload('currentTableId', {
                    page: {
                        curr: 1
                    },
                    where: {
                        searchParams: result
                    }
                }, 'data');

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') { // 监听添加操作
                    var index = layer.open({
                        title: '添加基础信息',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '../page/table-base/add.html',
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') { // 监听删除操作
                    var checkStatus = table.checkStatus('currentTableId')
                    data = checkStatus.data;
                    if (data.length > 0) {
                        // 给id数值格式化为适合查询的格式
                        var str = '';
                        $.each(data, function (i, val) {
                            var id = val.id
                            if (i != 0) {
                                id = "," + id;
                            }
                            str += id;
                        });
                        var id = {
                            "id": str
                        };
                        id = JSON.stringify(id);
                        layer.confirm('您确定要删除吗？', function (index) {
                            $.ajax({
                                url: '/system',
                                method: "DELETE",
                                data: id,
                                contentType: "application/json",
                                async: false,
                                error: function (err) {
                                    layer.msg(err.msg, {
                                        icon: 2
                                    });
                                }
                            });
                            tableIns.reload();
                            layer.close(index);
                        });
                    } else {
                        layer.msg("请至少选中一项！", {
                            icon: 2
                        });
                    }
                }
            });
            //监听指定开关
            form.on('switch(switchTest)', function (data) {
                var flag = (this.checked) ? flag = 0 : flag = 1;
                flag = JSON.stringify({
                    "flag": flag
                });
                $.ajax({
                    url: "/system/switch",
                    method: "PUT",
                    data: flag,
                    contentType: "application/json",
                    success: function (data) {
                        if (data.message)
                            layer.msg("切换成功！", {
                                icon: 1
                            });
                    },
                    error: function (err) {
                        layer.msg("服务器错误，请稍后重试!", {
                            icon: 2
                        });
                    }
                })
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                var id = {
                    "id": data.id
                };
                $.cookie("base_id", data.id);
                if (obj.event === 'edit') {

                    var index = layer.open({
                        title: '编辑基础信息',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '../page/table-base/edit.html',
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    id = JSON.stringify(id);
                    layer.confirm('您确定要删除吗？', function (index) {
                        $.ajax({
                            url: '/system',
                            method: "DELETE",
                            data: id,
                            contentType: "application/json",
                            error: function (err) {
                                layer.msg(err.msg, {
                                    icon: 2
                                });
                            }
                        })
                        obj.del();
                        layer.close(index);
                    });
                }
            });

        });
    </script>

</body>

</html>